package com.kuaifawu.lwnlawyerclient.Adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.kuaifawu.lwnlawyerclient.Activity.LWNQuestionDetailActivity;
import com.kuaifawu.lwnlawyerclient.Model.LWNModel_myGoodAtList;
import com.kuaifawu.lwnlawyerclient.R;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
import com.lidroid.xutils.bitmap.callback.BitmapLoadCallBack;
import com.lidroid.xutils.bitmap.callback.BitmapLoadFrom;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by zhangminglei on 15/1/5.
 */
public class LWNAdapter_myGoodAt extends ArrayAdapter {
    private LayoutInflater mInflater;
    private List<LWNModel_myGoodAtList> mItems;
    private Context context_this;

    public LWNAdapter_myGoodAt(Context context, int resource, List<LWNModel_myGoodAtList> objects) {
        super(context, resource, objects);
        context_this = context;
        this.mInflater = LayoutInflater.from(context);
        this.mItems = objects;
    }

    @Override
    public int getCount() {
        return mItems.size();
    }

    @Override
    public LWNModel_myGoodAtList getItem(int position) {
        return mItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

      final   LWNModel_myGoodAtList item = getItem(position);
        ArrayList<String> array_img = item.getImg_thumbnail();

        String[] string_location = {"北京", "上海", "天津", "重庆", "黑龙江省", "吉林省", "辽宁省", "河北省", "山西省", "内蒙古", "山东省",
                "安徽省", "江苏省", "浙江省", "江西省", "福建省", "河南省", "湖北省", "湖南省", "广东省", "广西", "海南省", "四川省", "贵州省", "云南省", "西藏", "陕西省", "甘肃省", "青海省", "宁夏", "新疆",
                "香港", "澳门", "台湾省"};

        int kind_content = Integer.parseInt(item.getQuestion_type());
        String location = string_location[Integer.parseInt(item.getQuestion_area())];

        View_content view_content = null;
        View_image view_image = null;

        if (convertView == null) {
            if(array_img == null) {
                convertView = mInflater.inflate(R.layout.list_wait_item, parent, false);
                view_content = new View_content();
                view_content.item_name = (TextView) convertView.findViewById(R.id.item_wait_name);
                view_content.item_lingyu = (TextView) convertView.findViewById(R.id.item_wait_lingyu);
                view_content.item_leixing = (TextView) convertView.findViewById(R.id.item_wait_leixing);
                view_content.item_time = (TextView) convertView.findViewById(R.id.item_wait_time);
                view_content.item_content = (TextView) convertView.findViewById(R.id.item_wait_content);
                convertView.setTag(R.layout.list_wait_item,view_content);
            }
            else {
                convertView = mInflater.inflate(R.layout.list_waitimage_item, parent, false);
                view_image = new View_image();
                view_image.item_name = (TextView) convertView.findViewById(R.id.item_wait_name);
                view_image.item_lingyu = (TextView) convertView.findViewById(R.id.item_wait_lingyu);
                view_image.item_leixing = (TextView) convertView.findViewById(R.id.item_wait_leixing);
                view_image.item_time = (TextView) convertView.findViewById(R.id.item_wait_time);
                view_image.item_content = (TextView) convertView.findViewById(R.id.item_wait_content);
                view_image.imageView_one = (ImageView) convertView.findViewById(R.id.item_wait_imageone);
                view_image.imageView_two = (ImageView) convertView.findViewById(R.id.item_wait_imagetwo);
                convertView.setTag(R.layout.list_waitimage_item,view_image);
            }
        }
        else{
            if(array_img == null){
                view_content = (View_content)convertView.getTag(R.layout.list_wait_item);
                if(view_content == null){
                    convertView = mInflater.inflate(R.layout.list_wait_item, parent, false);
                    view_content = new View_content();
                    view_content.item_name = (TextView) convertView.findViewById(R.id.item_wait_name);
                    view_content.item_lingyu = (TextView) convertView.findViewById(R.id.item_wait_lingyu);
                    view_content.item_leixing = (TextView) convertView.findViewById(R.id.item_wait_leixing);
                    view_content.item_time = (TextView) convertView.findViewById(R.id.item_wait_time);
                    view_content.item_content = (TextView) convertView.findViewById(R.id.item_wait_content);
                    convertView.setTag(R.layout.list_wait_item, view_content);
                }
            }
            else{
                view_image = (View_image)convertView.getTag(R.layout.list_waitimage_item);
                if(view_image == null){
                    convertView = mInflater.inflate(R.layout.list_waitimage_item, parent, false);
                    view_image = new View_image();
                    view_image.item_name = (TextView) convertView.findViewById(R.id.item_wait_name);
                    view_image.item_lingyu = (TextView) convertView.findViewById(R.id.item_wait_lingyu);
                    view_image.item_leixing = (TextView) convertView.findViewById(R.id.item_wait_leixing);
                    view_image.item_time = (TextView) convertView.findViewById(R.id.item_wait_time);
                    view_image.item_content = (TextView) convertView.findViewById(R.id.item_wait_content);
                    view_image.imageView_one = (ImageView) convertView.findViewById(R.id.item_wait_imageone);
                    view_image.imageView_two = (ImageView) convertView.findViewById(R.id.item_wait_imagetwo);
                    convertView.setTag(R.layout.list_waitimage_item, view_image);
                }
            }
        }

        //设置数据
        if(array_img != null){
            bitmapUtils = new BitmapUtils(context_this);
            if(array_img.size() == 2){
                view_image.imageView_two.setVisibility(View.VISIBLE);

                bitmapUtils.display(view_image.imageView_one, array_img.get(0));
                bitmapUtils.display(view_image.imageView_two, array_img.get(1));
                view_image.imageView_one.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                       // System.out.println("one");
                        //LWNQuestionDetailActivity qactivity=new LWNQuestionDetailActivity();
                    getPopupShowImg(item.getQuestion_img(),0);
                    }
                });

                view_image.imageView_two.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                       // System.out.println("two");
                       getPopupShowImg(item.getQuestion_img(),1);
                    }
                });
            }
            else{
                view_image.imageView_two.setVisibility(View.GONE);
                BitmapUtils bitmapUtils = new BitmapUtils(context_this);
                bitmapUtils.display(view_image.imageView_one, array_img.get(0));
                view_image.imageView_one.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        //System.out.println("one");
                       getPopupShowImg(item.getQuestion_img(),0);
                    }
                });
            }

            if (kind_content < 10) {
                view_image.item_leixing.setText("个人问题");
            } else {
                view_image.item_leixing.setText("公司问题");
            }
            view_image.item_name.setText(location + "网友");

            switch (kind_content) {
                case 0: {
                    view_image.item_lingyu.setText("婚姻家庭");
                }
                break;
                case 1: {
                    view_image.item_lingyu.setText("房屋买卖");
                }
                break;
                case 2: {
                    view_image.item_lingyu.setText("债务纠纷");
                }
                break;
                case 3: {
                    view_image.item_lingyu.setText("劳动争议");
                }
                break;
                case 4: {
                    view_image.item_lingyu.setText("交通肇事");
                }
                break;
                case 5: {
                    view_image.item_lingyu.setText("刑事案件");
                }
                break;
                case 6: {
                    view_image.item_lingyu.setText("其他类型");
                }
                break;
                case 11: {
                    view_image.item_lingyu.setText("公司注册");
                }
                break;
                case 12: {
                    view_image.item_lingyu.setText("投资融资");
                }
                break;
                case 13: {
                    view_image.item_lingyu.setText("股权期权");
                }
                break;
                case 14: {
                    view_image.item_lingyu.setText("合同事项");
                }
                break;
                case 15: {
                    view_image.item_lingyu.setText("知识产权");
                }
                break;
                case 16: {
                    view_image.item_lingyu.setText("财税事务");
                }
                break;
                case 17: {
                    view_image.item_lingyu.setText("劳动人事");
                }
                break;
                case 18: {
                    view_image.item_lingyu.setText("债务纠纷");
                }
                break;
                case 19: {
                    view_image.item_lingyu.setText("其他问题");
                }
                break;
                default:
                    break;
            }
            view_image.item_content.setText(item.getQuestion());
            view_image.item_time.setText(item.getTime());
        }
        else{
            if (kind_content < 10) {
                view_content.item_leixing.setText("个人问题");
            } else {
                view_content.item_leixing.setText("公司问题");
            }
            view_content.item_name.setText(location + "网友");
            switch (kind_content) {
                case 0: {
                    view_content.item_lingyu.setText("婚姻家庭");
                }
                break;
                case 1: {
                    view_content.item_lingyu.setText("房屋买卖");
                }
                break;
                case 2: {
                    view_content.item_lingyu.setText("债务纠纷");
                }
                break;
                case 3: {
                    view_content.item_lingyu.setText("劳动争议");
                }
                break;
                case 4: {
                    view_content.item_lingyu.setText("交通肇事");
                }
                break;
                case 5: {
                    view_content.item_lingyu.setText("刑事案件");
                }
                break;
                case 6: {
                    view_content.item_lingyu.setText("其他类型");
                }
                break;
                case 11: {
                    view_content.item_lingyu.setText("公司注册");
                }
                break;
                case 12: {
                    view_content.item_lingyu.setText("投资融资");
                }
                break;
                case 13: {
                    view_content.item_lingyu.setText("股权期权");
                }
                break;
                case 14: {
                    view_content.item_lingyu.setText("合同事项");
                }
                break;
                case 15: {
                    view_content.item_lingyu.setText("知识产权");
                }
                break;
                case 16: {
                    view_content.item_lingyu.setText("财税事务");
                }
                break;
                case 17: {
                    view_content.item_lingyu.setText("劳动人事");
                }
                break;
                case 18: {
                    view_content.item_lingyu.setText("债务纠纷");
                }
                break;
                case 19: {
                    view_content.item_lingyu.setText("其他问题");
                }
                break;
                default:
                    break;
            }
            view_content.item_content.setText(item.getQuestion());
            view_content.item_time.setText(item.getTime());
        }
        return convertView;
    }

    class View_content{
        private TextView item_name;
        private TextView item_lingyu;
        private TextView item_content;
        private TextView item_time;
        private TextView item_leixing;
    }

    class View_image{
        private TextView item_name;
        private TextView item_lingyu;
        private TextView item_content;
        private TextView item_time;
        private TextView item_leixing;
        private ImageView imageView_one;
        private ImageView imageView_two;
    }



    private ViewPager detail_show_image=null;
    private ImageButton detail_showback_image=null;
    private PopupWindow showpopupWindow=null;
    BitmapUtils bitmapUtils =null;
    private LinearLayout line_intor=null;
    private ImageButton line_intor_two=null;
    private ImageButton line_intor_one=null;
    private ProgressBar progress_bar;
    List<View> viewList=new ArrayList<View>();
    private void getPopupShowImg( final ArrayList<String> array_img,int imageindex){
        viewList.clear();
        View pView=mInflater.inflate(R.layout.popushow_image,null);
        detail_show_image=(ViewPager)pView.findViewById(R.id.detail_show_image);
        detail_showback_image=(ImageButton)pView.findViewById(R.id.detail_showback_image);
        line_intor=(LinearLayout)pView.findViewById(R.id.line_intor);
        progress_bar=(ProgressBar)pView.findViewById(R.id.progress_bar);
        if(array_img!=null){
            for(int i=0;i<array_img.size();i++){
                ImageView imageView=new ImageView(getContext());
                bitmapUtils.display(imageView, array_img.get(i),new BitmapLoadCallBack<ImageView>() {
                    @Override
                    public void onLoadCompleted(ImageView imageView, String s, Bitmap bitmap, BitmapDisplayConfig bitmapDisplayConfig, BitmapLoadFrom bitmapLoadFrom) {
                        progress_bar.setVisibility(View.GONE);
                        imageView.setImageBitmap(bitmap);
                    }

                    @Override
                    public void onLoadFailed(ImageView imageView, String s, Drawable drawable) {

                    }

                    @Override
                    public void onPreLoad(ImageView container, String uri, BitmapDisplayConfig config) {
                        super.onPreLoad(container, uri, config);
                        progress_bar.setVisibility(View.VISIBLE);
                    }
                });

                viewList.add(imageView);
            }
        }
        detail_show_image.setAdapter(new LWNAdapter_home(viewList));
        line_intor_one=(ImageButton)pView.findViewById(R.id.detail_showback_image);
        line_intor_two=(ImageButton)pView.findViewById(R.id.detail_showback_image);
//        line_intor_one.setOnClickListener();
//        line_intor_two.setOnClickListener(this);
        showpopupWindow=new PopupWindow(pView,
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT,true);
        showpopupWindow.setBackgroundDrawable(new BitmapDrawable());
        showpopupWindow.setOutsideTouchable(true);
        showpopupWindow.setFocusable(true);
        showpopupWindow.setAnimationStyle(R.style.showImagepopwindow_anim_style);

        showpopupWindow.showAtLocation(line_intor_one, Gravity.CENTER, 0, 0);
        detail_showback_image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(showpopupWindow.isShowing()){
                    showpopupWindow.dismiss();
                }
            }
        });
        line_intor.getChildAt(1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                detail_show_image.setCurrentItem(1);
                line_intor.getChildAt(1).setClickable(false);
                line_intor.getChildAt(0).setClickable(true);
            }
        });

        line_intor.getChildAt(0).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                detail_show_image.setCurrentItem(0);
                line_intor.getChildAt(0).setClickable(false);
                line_intor.getChildAt(1).setClickable(true);
            }
        });
        if(array_img.size()==1){
            line_intor.getChildAt(1).setVisibility(View.GONE);
        }else if(array_img.size()==2){
            line_intor.getChildAt(1).setVisibility(View.VISIBLE);
        }
        detail_show_image.setCurrentItem(imageindex);
        detail_show_image.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                if(position==0){
                    line_intor.getChildAt(position).setPressed(true);
                    line_intor.getChildAt(position+1).setPressed(false);
                    line_intor.getChildAt(position).setClickable(false);
                    line_intor.getChildAt(position+1).setClickable(true);
                }else{
                    line_intor.getChildAt(position).setPressed(true);
                    line_intor.getChildAt(position-1).setPressed(false);
                    line_intor.getChildAt(position).setClickable(false);
                    line_intor.getChildAt(position-1).setClickable(true);
                }
            }

            @Override
            public void onPageSelected(int position) {
//                if(position==0){
//                    line_intor.getChildAt(position).setPressed(true);
//                    line_intor.getChildAt(position+1).setPressed(false);
//                }else{
//                    line_intor.getChildAt(position).setPressed(true);
//                    line_intor.getChildAt(position-1).setPressed(false);
//                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
}
